package bl;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Parser {

	public String checkUpdates(boolean uptodate) throws IOException {
		String verxml = "<?xml version=\"1.0\"?>"
		+ "<homedroid>"
			+ "<checkUpdates>"
				+ "<version>" + uptodate + "</version>"
			+ "</checkUpdates>"
		+ "</homedroid>";
		return verxml;
	}
	public String sendAcknowledge(String message) throws IOException {
		String regStatusxml = "<?xml version=\"1.0\"?>" +
			"<homedroid>" +
				"<acknowledge>" +
					"<message>" + message + "</message>" +
				"</acknowledge>" +
			"</homedroid>";
		
		return regStatusxml;
	}
	public String sendProductDescription(String barcode, String label, String brand, String content_, String content_2, String unit) throws IOException {
		String sendproductdescriptionxml = "<?xml version=\"1.0\"?>" +
			"<homedroid>" +
				"<sendProductDescription>" +
					"<barcode>" + barcode + "</barcode>" +
					"<label>" + label + "</label>" +
					"<brand>" + brand + "</brand>" +
					"<content_>" + content_ + "</content_>" +
					"<content_2>" + content_2 + "</content_2>" +
					"<unit>" + unit + "</unit>" +
				"</sendProductDescription>" +
			"</homedroid>";
		
		return sendproductdescriptionxml;
	}
	public String parseOnStockProduct(String actual_amount, String barcode, String label, String brand, String content_, String content_2, String unit) throws IOException {
		String onStockProductxml =
				"<actual_amount>" + actual_amount + "</actual_amount>" +
				"<barcode>" + barcode + "</barcode>" +
				"<label>" + label + "</label>" +
				"<brand>" + brand + "</brand>" +
				"<content_>" + content_ + "</content_>" +
				"<content_2>" + content_2 + "</content_2>" +
				"<unit>" + unit + "</unit>";
		
		return onStockProductxml;
	}
	
	
	public String getList(StringBuffer productBuffer) throws IOException{
		String[] products = productBuffer.toString().split(":");
		String[] stringlist = new String[products.length];
		StringBuffer buffer = new StringBuffer();
		for(int i = 1; i < products.length; i++){
			String[]items = products[i].toString().split(",");
			stringlist[i] = "<item>" + i + "</item>" +
					"<amount>" + items[0] + "</amount>" +
					"<barcode>" + items[1] + "</barcode>" +
					"<label>" + items[2] + "" + "</label>" +
					"<brand>" + items[3] + "</brand>" +
					"<content_>" + items[4] + "</content_>" +
					"<content_2>" + items[5] + "</content_2>" +
					"<unit>" + items[6] + "</unit>";
								
			buffer.append(stringlist[i]);
		}
		String productxml = "<?xml version=\"1.0\"?>" +
				"<homedroid>" +
					"<getList>" +
						buffer +
					"</getList>" +
				"</homedroid>";
		return productxml;
				
	}
	public String getAlreadyOnStockList(StringBuffer onStockBuffer) throws IOException{
		String getAlreadyOnStockList = "<?xml version=\"1.0\"?>" +
				"<homedroid>" +
				"<getAlreadyOnStockList>" +
				onStockBuffer +
				"</getAlreadyOnStockList>" +
				"</homedroid>";
		return getAlreadyOnStockList;
		
	}
	
	public void xmlread(String toEncode, final User user, final Product product, final List onstock){
		  try {
		 
		     SAXParserFactory factory = SAXParserFactory.newInstance();
		     SAXParser saxParser = factory.newSAXParser();
		 
		     DefaultHandler handler = new DefaultHandler() {
		 
		     boolean version,username,password,item,amount,barcode,label,brand,content_,content_2,u_id = false;
		     int auswahl = 0;
		 
		     public void startElement(String uri, String localName,
		    	String qName, Attributes attributes)
		        throws SAXException {
		    	if (qName.equalsIgnoreCase("regUser")){
		    		user.setRequest("regUser");
		    		auswahl = 1;
		    	}
		    	if (qName.equalsIgnoreCase("loginUser")){
		    		user.setRequest("loginUser");
		    		auswahl = 1;
		    	}
		    	if (qName.equalsIgnoreCase("getList")){
		    		user.setRequest("getList");
		    		auswahl = 1;
		    	}
		    	if (qName.equalsIgnoreCase("sendBarcode")){
		    		user.setRequest("sendBarcode");
		    		auswahl = 2;
		    	}
		    	if (qName.equalsIgnoreCase("addProduct")){
		    		user.setRequest("addProduct");
		    		auswahl = 3;
		    	}
		    	if (qName.equalsIgnoreCase("sendList")){
		    		user.setRequest("sendList");
		    		auswahl = 4;
		    	}
		    	if (qName.equalsIgnoreCase("outOfStock")){
		    		user.setRequest("outOfStock");
		    		auswahl = 4;
		    	}
		    	if (qName.equalsIgnoreCase("checkUpdates")){
		    		user.setRequest("checkUpdates");
		    		auswahl = 5;
		    	}
		    	
		    	
//###########################################################################################
		    	
		    	if (qName.equalsIgnoreCase("version")) {
		    		version = true;
		    	}
		    	if (qName.equalsIgnoreCase("username")) {
		    		username = true;
		    	}
		    	if (qName.equalsIgnoreCase("password")) {
		    		password = true;
		    	}
		    	if (qName.equalsIgnoreCase("item")) {
		    		item = true;
		    	}
		    	if (qName.equalsIgnoreCase("amount")) {
		    		amount = true;
		    	}
		    	if (qName.equalsIgnoreCase("barcode")) {
		    		barcode = true;
		    	}
		    	if (qName.equalsIgnoreCase("label")) {
		    		label = true;
		    	}
		    	if (qName.equalsIgnoreCase("brand")) {
		    		brand = true;
		    	}
		    	if (qName.equalsIgnoreCase("content_")) {
		    		content_ = true;
		    	}
		    	if (qName.equalsIgnoreCase("content_2")) {
		    		content_2 = true;
		    	}		 
		    	if (qName.equalsIgnoreCase("u_id")) {
		    		u_id = true;
		    	}	
		    	
		     }
		     public void characters(char ch[], int start, int length)
		     
		        throws SAXException {
		    	switch (auswahl) {
				case 1:
					if (username) {
						user.setUsername(new String(ch, start, length));
						username = false;
					} 
					if (password) {
						user.setPassword(new String(ch, start, length));
						password = false;
					} 
					break;
				case 2:
					if (barcode) {
						product.setBarcode(new String(ch, start, length));
						barcode = false;
					} 
					break;
				case 3:
					if (barcode) {
						product.setBarcode(new String(ch, start, length));
						barcode = false;
					} 
					if (label) {
						product.setLabel(new String(ch, start, length));
						label = false;
					} 
					if (brand) {
						product.setBrand(new String(ch, start, length));
						brand = false;
					} 
					if (content_) {
						product.setContent_(Integer.parseInt(new String(ch, start, length)));
						content_ = false;
					} 
					if (content_2) {
						product.setContent_2(Integer.parseInt(new String(ch, start, length)));
						content_2 = false;
					} 
					if (u_id) {
						product.setU_id(Integer.parseInt(new String(ch, start, length)));
						u_id = false;
					} 
					break;
				case 4:
					if (item) {
						product.setItem(Integer.parseInt(new String(ch, start, length)));
						item = false;
					} 
					if (amount) {
						product.setAmount(Integer.parseInt(new String(ch, start, length)));
						amount = false;
					} 
					if (barcode) {
						product.setBarcode(new String(ch, start, length));
						barcode = false;
					} 
					if (username) {
						user.setUsername(new String(ch, start, length));
						username = false;
					} 
					if (password) {
						user.setPassword(new String(ch, start, length));
						password = false;
					} 
					break;
				case 5:
					if (version) {
						user.setVersion(new String(ch, start, length));
						version = false;
					}

					
				default:
					break;
				}
		        }
		 
		      };
		      StringReader inStream = new StringReader(toEncode);
		      InputSource inSource = new InputSource(inStream);
		      saxParser.parse(inSource, handler);
		      
		    } catch (Exception e) {
		    	System.out.println("ParserError");
		        e.printStackTrace();
		    }
	}
}
